﻿#include "FITKAbaqusAdaptorConnectorLockBehavior.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "InpReadInformationMapper.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractAssInstance.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConnectorSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConnectorBehaviorLock.h"
#include <QDebug>
//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorConnectorLockBehavior::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorConnectorLockBehavior";
    }

    bool FITKAbaqusAdaptorConnectorLockBehavior::adaptR()
    {
        _sectionConnectorData = dynamic_cast<Interface::FITKAbaConnectorSection*>(_dataObj);
        if (!_sectionConnectorData || !_reader)return false;
        //回退一行，处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*connector lock")) return false;
        //添加行为
        _lockBehaviorData = dynamic_cast<Interface::FITKAbaConnectorBehaviorLock*>
            (_sectionConnectorData->addBehavior(Interface::FITKAbaConnectorBehaviorEnum::ConnectorBehaviorType::Lock));
        if (!_lockBehaviorData)return false;
        bool readOK = false;
        InpKeyLineSpliter s(line, _reader);
        //力矩类型
        this->_component = s["component"].trimmed().toInt(&readOK);
        if (!readOK)return false;
        if (_component < 1 || _component > 6)return false;
        std::array<bool, 6> componentsState{ false, false, false, false, false, false };
        componentsState[_component - 1] = true;
        _lockBehaviorData->setComponents(componentsState);
        //lock
        if (s.hasKey("lock"))
        {
            _lockBehaviorData->setLockType(Interface::FITKAbaConnectorBehaviorEnum::LockType::Specify);
            int lockNum = s["lock"].trimmed().toInt(&readOK);
            if (!readOK)return false;
            _lockBehaviorData->setLockSpecify(Interface::FITKAbaConnectorBehaviorEnum::LockSpecify(lockNum));
        }
        //读取stop的数据
        readOK = this->readLockData();
        //读取失败就删除
        if (!readOK)
            _sectionConnectorData->removeBehavior(_lockBehaviorData);
        return readOK;
    }

    bool FITKAbaqusAdaptorConnectorLockBehavior::adaptW()
    {
        _sectionConnectorData = dynamic_cast<Interface::FITKAbaConnectorSection*>(_dataObj);
        if (!_sectionConnectorData || !_lockBehaviorData || !_writer)return false;
        bool writeOK = false;
        QTextStream* stream = _writer->getStream();
        std::array<bool, 6> componentsState = _lockBehaviorData->getComponents();
        for (int i = 0; i < 6; ++i)
        {
            if (!componentsState[i])continue;
            QString lineOne = QString("*Connector Lock, component=%1").arg(i + 1);
            if (_lockBehaviorData->getLockType() == Interface::FITKAbaConnectorBehaviorEnum::LockType::Specify)
                lineOne += QString(", lock=%1").arg(int(_lockBehaviorData->getLockSpecify()));
            *stream << lineOne << endl;
            writeOK = this->writeLockBehavior();
            if (!writeOK)return false;
        }
        return true;
    }

    void FITKAbaqusAdaptorConnectorLockBehavior::setWriteBehaviorData(Interface::FITKAbaConnectorBehaviorLock* lockData)
    {
        _lockBehaviorData = lockData;
    }

    bool FITKAbaqusAdaptorConnectorLockBehavior::readLockData()
    {
        if (!_lockBehaviorData || !_reader)return false;
        QString line = _reader->readLine().trimmed();
        QStringList str = line.split(',');
        if (str.size() != 4)return false;
        bool ok = false;
        _lockBehaviorData->setUsePosLower(false);
        _lockBehaviorData->setUsePosUpper(false);
        _lockBehaviorData->setUseFMLower(false);
        _lockBehaviorData->setUseFMUpper(false);
        //获取位置下边界
        double lowerBoundPosition = 0.0;
        if (!str.at(0).trimmed().isEmpty())
        {
            lowerBoundPosition = str.at(0).trimmed().toDouble(&ok);
            if (!ok)return false;
            _lockBehaviorData->setUsePosLower(true);
        }
        //获取位置上边界
        double upperBoundPosition = 0.0;
        if (!str.at(1).trimmed().isEmpty())
        {
            upperBoundPosition = str.at(1).trimmed().toDouble(&ok);
            if (!ok)return false;
            _lockBehaviorData->setUsePosUpper(true);
        }
        if (_lockBehaviorData->getUsePosUpper())
            if (upperBoundPosition <= lowerBoundPosition)return false;
        //获取力矩下边界
        double lowerBoundForceM = 0.0;
        if (!str.at(2).trimmed().isEmpty())
        {
            lowerBoundForceM = str.at(2).trimmed().toDouble(&ok);
            if (!ok)return false;
            _lockBehaviorData->setUseFMLower(true);
        }
        //获取力矩上边界
        double upperBoundForceM = 0.0;
        if (!str.at(3).trimmed().isEmpty())
        {
            upperBoundForceM = str.at(3).trimmed().toDouble(&ok);
            if (!ok)return false;
            _lockBehaviorData->setUseFMUpper(true);
        }
        if (_lockBehaviorData->getUseFMUpper())
            if (upperBoundForceM <= lowerBoundForceM)return false;
        //设置边界
        _lockBehaviorData->setPosCriteriaLower(lowerBoundPosition);
        _lockBehaviorData->setPosCriteriaUpper(upperBoundPosition);
        _lockBehaviorData->setFMCriteriaLower(lowerBoundForceM);
        _lockBehaviorData->setFMCriteriaUpper(upperBoundForceM);
        //再次读取一行
        _reader->readLine();
        return true;
    }

    bool FITKAbaqusAdaptorConnectorLockBehavior::writeLockBehavior()
    {
        if (!_writer || !_lockBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        //获取位置下边界
        QString lowerBoundPosition, upperBoundPosition, lowerBoundForceM, upperBoundForceM;
        if (_lockBehaviorData->getUsePosLower())
            lowerBoundPosition = _writer->double2String(_lockBehaviorData->getPosCriteriaLower(), 8, 0);
        //获取位置上边界
        if (_lockBehaviorData->getUsePosUpper())
            upperBoundPosition = _writer->double2String(_lockBehaviorData->getPosCriteriaUpper(), 8, 0);
        //获取力矩下边界
        if (_lockBehaviorData->getUseFMLower())
            lowerBoundForceM = _writer->double2String(_lockBehaviorData->getFMCriteriaLower(), 8, 0);
        //获取力矩上边界
        if (_lockBehaviorData->getUseFMUpper())
            upperBoundForceM = _writer->double2String(_lockBehaviorData->getFMCriteriaUpper(), 8, 0);
        *stream << QString("%1, %2, %3, %4").arg(lowerBoundPosition).arg(upperBoundPosition)
            .arg(lowerBoundForceM).arg(upperBoundForceM) << endl;
        return true;
    }
}